#include "etaview.h"
#include <QHBoxLayout>
#include <QApplication>
#include <QMouseEvent>
#include <QBitmap>
#include "common.h"

int EtaView::m_id = qRegisterMetaType<EtaView*>();

EtaView::EtaView(QWidget *parent)
    :ViewObject(parent)
    ,m_label(NULL)
    ,m_paramEtaView(NULL)
{
    setWindowFlags(Qt::FramelessWindowHint);
    setAttribute(Qt::WA_DeleteOnClose);

    m_label = new QLabel(this);
    m_label->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
    m_label->setAlignment(Qt::AlignCenter);
    m_layout->addWidget(m_label, 0, Qt::AlignCenter);

#ifdef __linux__
    setFocusPolicy(Qt::NoFocus);
#else
    setFocusPolicy(Qt::StrongFocus);
#endif

    if (m_objectParam == NULL)
        m_objectParam = new ParamObject;

    if (m_paramEtaView == NULL)
        m_paramEtaView = new EtaViewParam;

}

EtaView::~EtaView()
{
    if (m_paramEtaView != NULL)
    {
        delete m_paramEtaView;
        m_paramEtaView = NULL;
    }
    if (m_objectParam != NULL)
    {
        delete m_objectParam;
        m_objectParam = NULL;
    }
}

void EtaView::ShowObject()
{

    if (m_paramEtaView == NULL || m_objectParam == NULL) return;

    int stid = m_objectParam ->GetStId();

    QVector<int> time_vector;

    if (stid != 0 && mProtocolData)
    {
        int curr_id  = mProtocolData->curr_or_next_id;
        int start_id = mProtocolData->start_id;
        int stop_id  = mProtocolData->stop_id;
        int rundir = RUN_INVALID;
        if (start_id < stop_id)
            rundir = RUN_UP;
        else if (start_id > stop_id)
            rundir = RUN_DOWN;

        if (mProtocolData->eta_list.size() != 0 && curr_id != 0)
            foreach (int time, mProtocolData->eta_list)
                time_vector.push_back(time);
        else
            m_paramEtaView->m_textContent = "";

        if ((stid >= curr_id && rundir == RUN_UP) || (stid <= curr_id && rundir == RUN_DOWN))
            //到站图非开门侧全线图当前站点是不需要显示到站时间的，若没此图，则正常显示
            if (!(stid == curr_id && mProtocolData->ui_type == ArriveAllFlag) && mProtocolData->eta_list.size() != 0 && curr_id != 0)
                m_paramEtaView->m_textContent = QString::number(time_vector.at(stid - 1));
            else
                m_paramEtaView->m_textContent = "";
        else
            m_paramEtaView->m_textContent = "";

        if (rundir == RUN_UP)
        {
            if (stid < start_id || stid > stop_id)
                m_paramEtaView->m_textContent = "";
        } else if (rundir == RUN_DOWN)
        {
            if (stid > start_id || stid < stop_id)
                m_paramEtaView->m_textContent = "";
        }
        foreach (int id, mProtocolData->skip_list)
        {
            if (id == stid)
            {
                m_paramEtaView->m_textContent = "";
                break;
            }
        }
    }
    else
    {
#ifdef __WIN32__
        for (int i = 0; i < myapp::mDynamicMapParam->m_station_total; i++)
            time_vector.push_back(2);
        m_paramEtaView->m_textContent = QString::number(time_vector.at(stid - 1));
#endif
    }

    m_label->setText(m_paramEtaView->m_textContent);

    location(m_objectParam->GetX(), m_objectParam->GetY(), m_objectParam->GetWidth(), m_objectParam->GetHeight());
    m_label->setFixedSize(size());

    show();
}

void EtaView::GetAttribute(std::string& attribute)
{    
    std::string jsonStr;
    Json::Value base;
    Json::StreamWriterBuilder writerBuilder;
    std::ostringstream os;

    base["zindex"] = QString::number(m_objectParam->GetIndex()).toStdString();
    base["stid"] = QString::number(m_objectParam->GetStId()).toStdString();
    base["x"] = QString::number(m_objectParam->GetX()).toStdString();
    base["y"] = QString::number(m_objectParam->GetY()).toStdString();
    base["w"] = QString::number(m_objectParam->GetWidth()).toStdString();
    base["h"] = QString::number(m_objectParam->GetHeight()).toStdString();
    base["name"] = m_objectParam->GetName().toStdString();

    std::unique_ptr<Json::StreamWriter> jsonWriter(writerBuilder.newStreamWriter());
    jsonWriter->write(base, &os);
    jsonStr = os.str();

    attribute = jsonStr;
}

void EtaView::SetAttribute(std::string jsonStr)
{
    bool res;
    JSONCPP_STRING errs;
    Json::Value root;
    Json::CharReaderBuilder readerBuilder;
    std::unique_ptr<Json::CharReader> const jsonReader(readerBuilder.newCharReader());
    res = jsonReader->parse(jsonStr.c_str(), jsonStr.c_str()+jsonStr.length(), &root, &errs);

    if (!res || !errs.empty())
         LOG_QDEBUG<<__FUNCTION__<<"json parse faild"<<QString::fromStdString(errs);

    if (CheckJsonValueType(root,"zindex", Json::stringValue))
        m_objectParam->SetIndex(QString::fromStdString(root["zindex"].asString()).toInt());

    if (CheckJsonValueType(root,"stid",Json::stringValue))
        m_objectParam->SetStId(QString::fromStdString(root["stid"].asString()).toInt());

    if (CheckJsonValueType(root,"x",Json::stringValue))
        m_objectParam->SetX(QString::fromStdString(root["x"].asString()).toInt());

    if (CheckJsonValueType(root,"y",Json::stringValue))
        m_objectParam->SetY(QString::fromStdString(root["y"].asString()).toInt());

    if (CheckJsonValueType(root,"w",Json::stringValue))
         m_objectParam->SetWidth(QString::fromStdString(root["w"].asString()).toInt());

    if (CheckJsonValueType(root,"h",Json::stringValue))
         m_objectParam->SetHeight(QString::fromStdString(root["h"].asString()).toInt());

    if (CheckJsonValueType(root,"name",Json::stringValue))
         m_objectParam->SetName(QString::fromStdString(root["name"].asString()));

    if (CheckJsonValueType(root,"stid",Json::stringValue))
        m_objectParam->SetStId(QString::fromStdString(root["stid"].asString()).toInt());

}

void EtaView::ParseXML(QDomElement* xmlDomEle)
{
    if (m_paramEtaView == NULL)
        m_paramEtaView = new EtaViewParam;

    m_objectParam->SetIndex((xmlDomEle->attribute("zindex","0")).toInt());
    m_objectParam->SetStId((xmlDomEle->attribute("stid","0")).toInt());
    m_objectParam->SetX((xmlDomEle->attribute("x","0")).toInt());
    m_objectParam->SetY((xmlDomEle->attribute("y","0")).toInt());
    m_objectParam->SetWidth((xmlDomEle->attribute("w","0")).toInt());
    m_objectParam->SetHeight((xmlDomEle->attribute("h","0")).toInt());
    m_objectParam->SetName(xmlDomEle->attribute("name, "));
}

void EtaView::PacketXML(QDomElement* dom)
{
    QDomDocument document;
    *dom = document.createElement("item");
    dom->setAttribute("type", m_objectParam->GetObjectName());
    dom->setAttribute("zindex",QString::number(m_objectParam->GetIndex()));
    dom->setAttribute("stid", QString::number(m_objectParam->GetStId()));
    dom->setAttribute("x",QString::number(m_objectParam->GetX()));
    dom->setAttribute("y",QString::number(m_objectParam->GetY()));
    dom->setAttribute("w",QString::number(m_objectParam->GetWidth()));
    dom->setAttribute("h",QString::number(m_objectParam->GetHeight()));
    dom->setAttribute("name",m_objectParam->GetName());
}
